package com.enjoyor.soa.traffic.server.efoms.helper;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

/**
 * 列出FTP服务器上指定目录下面的所有文件
 */
public class FTPAllFilesHelper {
    public         FTPClient         ftp;
    //public         ArrayList<String> arFiles;

    public static void getFTPFiles() throws IOException {
        FTPAllFilesHelper ftpFilesHelper = new FTPAllFilesHelper(true);
        List<String> arFiles = new ArrayList<String>();
        if (ftpFilesHelper.login("172.10.168.246", 21, "enjoyor", "enjoyor")) {
        	arFiles = ftpFilesHelper.ListFiles("/20190508/");
        }
        ftpFilesHelper.disConnection();
        for (String arFile : arFiles) {
            System.out.println("------------"+arFile);
        }
    }
    
    /**
     * 重载构造函数
     *
     * @param isPrintCommmand 是否打印与FTPServer的交互命令
     */
    public FTPAllFilesHelper(boolean isPrintCommmand) {
        ftp = new FTPClient();
        //arFiles = new ArrayList<String>();
        if (isPrintCommmand) {
            ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
        }
    }
  
    /**
     * 登陆FTP服务器
     *
     * @param host     FTPServer IP地址
     * @param port     FTPServer 端口
     * @param username FTPServer 登陆用户名
     * @param password FTPServer 登陆密码
     * @return 是否登录成功
     * @throws IOException
     */
    public boolean login(String host, int port, String username, String password) throws IOException {
        this.ftp.connect(host, port);
        if (FTPReply.isPositiveCompletion(this.ftp.getReplyCode())) {
            if (this.ftp.login(username, password)) {
                this.ftp.setControlEncoding("GBK");
                return true;
            }
        }
        if (this.ftp.isConnected()) {
            this.ftp.disconnect();
        }
        return false;
    }

    /**
     * 关闭数据链接
     *
     * @throws IOException
     */
    public void disConnection() throws IOException {
        if (this.ftp.isConnected()) {
            this.ftp.disconnect();
        }
    }

    /**
     * 递归遍历出目录下面所有文件
     *
     * @param pathName 需要遍历的目录，必须以"/"开始和结束
     * @throws IOException
     */
    public List<String> ListFiles(String pathName) throws IOException {
    	List<String> arFiles = new ArrayList<String>();
        if (pathName.startsWith("/") && pathName.endsWith("/")) {
            //更换目录到当前目录
        	boolean flag = this.ftp.changeWorkingDirectory(pathName);
            if(!flag) {
        		return arFiles;
        	}
            this.ftp.enterLocalPassiveMode();
            FTPFile[] files = this.ftp.listFiles();
            for (FTPFile file : files) {
                if (file.isFile()) {
                    arFiles.add(file.getName());
                }
            }
        }
        return arFiles;
    }
}